<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdapterWebController;
use App\Modules\Register\Register;
use Illuminate\Hashing\BcryptHasher;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class RegisterController extends AdapterWebController
{
    // 注册页面
    public function register(Request $request)
    {
        if ($request->isMethod('get')) {
            return view('admin.register.register');
        }
        $this->validate($request, [
            'country' => ['required', Rule::in(['中国澳门', '中国大陆', '中国香港', '中国台湾', '海外地区'])],
            'language' => ['required', Rule::in([1,2,3,4])],
            'type' => ['required', Rule::in([1,2,3,4])]
        ]);
        if(!empty($request['software_id'])){
            $request['software_id'] = strtoupper($request['software_id']);
        }
        if ($request['type'] == 1) {
            return view('admin.register.musician_info', $request);
        }
        if ($request['type'] == 2) {
            return view('admin.register.group_info', $request);
        }
        if ($request['type'] == 3) {
            return view('admin.register.broker_info', $request);
        }
        if ($request['type'] == 4) {
            return view('admin.register.company_info', $request);
        }
    }

    // 注册信息
    public function registerInfo(Request $request)
    {

        $postData = array_filter($request->all());
        if (isset($postData['business_license'])) {
            $fileRes = $this->fileUpload($request, 'business_license');
            if (!$fileRes['status']) {
                flash('请上传营业执照')->success();
            }
            $postData['business_license'] = $fileRes['url'];
        }
        $rst = $this->appLayerExec(Register::class, 'register', $postData);
        if ($rst['status'] == 1) {
           // return redirect('admin/password/' . $rst['res']['id']);
            return redirect('admin/hint/' . $rst['res']['id']);
        }
        return redirect()->back();
    }

    // 注册密码
//    public function password(Request $request)
//    {
//        if ($request->isMethod('get')) {
//            return view('admin.register.password', ['id' => $request['id']]);
//        }
//        $this->validate($request, [
//            'password' => 'required|confirmed|min:3|max:16'
//        ]);
//
//        $request['password'] = (new BcryptHasher())->make($request['password']);
//        $rst = $this->appLayerExec(Register::class, 'edit', ['id' => $request['id'],
//            'password' => $request['password']]);
//        if ($rst['res']['autoRes'] == 1) {
//            return redirect('admin/hint/' . $request['id']);
//        }
//    }

    // 温馨提示
    public function hint(Request $request)
    {
        if ($request->isMethod('get')) {
            return view('admin.register.hint', ['id' => $request['id']]);
        }
        $this->validate($request, [
            'phone_time' => ['required', Rule::in([1, 2, 3, 4])]
        ]);
        $rst = $this->appLayerExec(Register::class, 'edit', ['id' => $request['id'],
            'phone_time' => $request['phone_time']]);
        if ($rst['res']['autoRes'] == 1) {
            flash('恭喜！注册已成功')->success();
            return back()->withInput();
        }
    }
}